package com.architecture.util.bundle;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

import com.architecture.util.object.ObjectUtility;

/**
 * <p>This class has utilities methods to work with bundles and I8CN.</p>
 * @author JR
 * @version 1.0
 * @category Bundle Utilities
 */
public final class BundleUtility{

	/**
	 * <p>Represents a properties file.</p>
	 */
	private Properties properties;
	
	private BundleUtility() {
		super();
	}
	
	/**
	 * <p>Returns a new instance of a @{link BundleUtility}.</p>
	 * @return <code>BundleUtility</code>
	 */
	public static BundleUtility getInstance() {
		return new BundleUtility();
	}

	/**
	 * <p>Build bundle utility.</p>
	 * @param in
	 * @return <code>BundleUtility</code>
	 */
	public BundleUtility build(InputStream in) {
		if(ObjectUtility.isNotRef(in)){
			throw new NullPointerException("The input stream can't be null.");
		}
		this.properties = new Properties();
		try {
			this.getProperties().load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this;
	}

	/**
	 * <p>Returns the value of a specified key.</p>
	 * @param key
	 * @return <code>String</code>
	 */
	public String getValue(final String key){
		if(ObjectUtility.isNotRef(this.getProperties())){
			throw new NullPointerException();
		}
		return this.getProperties().getProperty(key);
	}
	
	/**
	 * <p>Returns the value of a specified key and format message using values specified in the second parameter.</p>
	 * @param key
	 * @param values
	 * @return <code>String</code>
	 */
	public String getValue(final String key,final Object...values){
		return MessageFormat.format(this.getValue(key),values);
	}

	/**
	 * @return <code>Properties</code>
	 */
	private Properties getProperties() {
		return properties;
	}
}
